package com.gz.logistics.library;

import android.content.Context;
import android.util.Log;

import com.gz.logistics.library.concurrent.ThreadExecutor;
import com.gz.logistics.library.dao.UserInfoDao;
import com.gz.logistics.library.listener.LoginListener;
import com.gz.logistics.library.pojo.UserInfo;
import com.gz.logistics.library.util.Constants;
import com.gz.logistics.library.util.PreferenceModel;
import com.gz.logistics.library.util.SingtonAsyncHttpClient;
import com.gz.logistics.library.util.Tools;
import com.loopj.android.http.RequestParams;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 账户管理相关操作接口
 * 账户登录、注销等一些和账户相关的操作接口
 * author wanner1991
 */
public class IAuthor {
    private static final String TAG = IAuthor.class.getSimpleName();
    private static IAuthor mInstance;
    private static Lock mLock;
    private Context mContext;
    private UserInfo userInfo;

    static{
        mLock = new ReentrantLock();
    }

    private IAuthor(Context mContext) {

        this.mContext = mContext;
    }

    public static IAuthor getInstance(){
        return mInstance;
    }

    public static void init(Context context){
        if (mInstance == null){
            mLock.lock();
            try {
                if (mInstance == null){
                    mInstance = new IAuthor(context);
                }
            } finally {
                mLock.unlock();
            }
        }
    }

    /**
     * 登录
     * @param user 用户名
     * @param pw   密码
     * @param loginListener 回调接口
     */
    public void login(String user,String pw,final LoginListener loginListener){
        if (!Tools.hasInternetConnection(mContext)){
            loginListener.onNoNetWork();
            return ;
        }
        RequestParams params = new RequestParams();
        params.put("user",user);
        params.put("pw",pw);

        SingtonAsyncHttpClient.getInstance().post(Constants.URL_LOGIN,params,loginListener);
        Log.i("summer",Constants.URL_LOGIN+"?"+params);

    }

    /**
     *根据用户ID 查询指定用户
     * @param uid 用户id
     * @return
     */
    public UserInfo loadUserInfoSyn(int uid){
        if (uid != -1){
            UserInfoDao dao = new UserInfoDao(mContext);
            return dao.queryByUid(uid);
        }
        return null;
    }

    /**
     * 保存用户信息到数据库
     * @param userInfo
     */
    public  void saveToDB(final UserInfo userInfo){
        ThreadExecutor.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                if (userInfo != null){
                    PreferenceModel.putInt(Constants.PREFERENCE_USER_ID, userInfo.getUser_id());
                    UserInfoDao dao  = new UserInfoDao(mContext);
                    dao.delete();
                    dao.insert(userInfo);
                }
            }
        });
    }

    /**
     * 清除用户信息
     */
    public void cleanUserInfo(){
        ThreadExecutor.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                PreferenceModel.remove(Constants.PREFERENCE_USER_ID);
                UserInfoDao dao  = new UserInfoDao(mContext);
                dao.delete();
            }
        });
    }


}
